package com.dd.keel.core.common.utils;

import java.lang.Runtime;
import java.lang.Math;

/**
 * JVM monitor
 * @author Kevin.XU
 *
 */
public class JVMMonitor {
	
	int cpu_num    = 0;
	long max_mem   = 0L;
	long total_mem = 0L;
	long free_mem  = 0L;
	long used_mem  = 0L;
	
	StringBuilder sb = new StringBuilder();
	
	public void set()
	{
		if(cpu_num==0) cpu_num = Runtime.getRuntime().availableProcessors();
		max_mem = Runtime.getRuntime().maxMemory()/1024;
		total_mem = Runtime.getRuntime().totalMemory()/1024;
		free_mem = Runtime.getRuntime().freeMemory()/1024;
		used_mem = total_mem - free_mem;
	}
	
	public String info()
	{
		set();
		sb.delete(0, sb.length());
		sb.append("cpu:").append(cpu_num).append(",");
		sb.append("max:").append(max_mem).append("k,");
		sb.append("total:").append(total_mem).append("k,");
		sb.append("used:").append(used_mem).append("k,");
		sb.append("free:").append(free_mem).append("k,");
		double used_rate = ((double)used_mem/total_mem) * 100;
		sb.append("used%:").append( MathUtil.round(used_rate, 2) ).append("%");
		return sb.toString();
	}
	
}
